package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.company.CompanyApplyHandleFormBean;
import com.info.zhiduoduo.admin.model.formbean.company.CompanyApplyQueryFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.exception.Asserts;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.company.CompanyAccount;
import com.info.zhiduoduo.repository.entity.company.CompanyApply;
import com.info.zhiduoduo.repository.mapper.company.CompanyApplyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tianyonghong
 * @create 2023/9/14
 */
@Slf4j
@Service
public class CompanyApplyService extends BaseService {

    @Resource
    private CompanyApplyMapper companyApplyMapper;

    public SmartResult queryList(BaseSimpleFormBean<CompanyApplyQueryFormBean> formBean) {
        CompanyApplyQueryFormBean req = formBean.getReqdata();
        Page<CompanyApply> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<CompanyApply> list = companyApplyMapper.queryList(req.getCompid(), req.getCompname(), req.getType(), req.getState(), req.getContent());
        return smartResultPage(page);
    }

    public SmartResult handle(BaseSimpleFormBean<CompanyApplyHandleFormBean> formBean){
        CompanyApplyHandleFormBean req = formBean.getReqdata();
        // 查询数据
        CompanyApply companyApply = new CompanyApply().selectById(req.getId());
        Asserts.notNull(companyApply, StatusMsg.StatusMsg_102);
        Asserts.isTrue(companyApply.getState() == 1, StatusMsg.StatusMsg_199);

        // 修改状态
        companyApply.setState(req.getState())
                    .setHandleAdminId(formBean.getUserid())
                    .setHandleReason(req.getHandleReason())
                    .setHandleTime(new Date())
                    .updateById();

        // 通过 修改发票内容
        if(req.getState() == 2){
            Company company = new Company().selectById(companyApply.getCompid());
            Asserts.notNull(companyApply, StatusMsg.StatusMsg_302);
            if (companyApply.getType() == 1) {
                company.setInvoiceScope(req.getInvoiceContent()).updateById();
            } else if (companyApply.getType() == 2) {
                company.setInvoiceScopeOdd(req.getInvoiceContent()).updateById();
            }
        }

       return SmartResult.success();
    }
}
